#include <string.h>
#include <curl/curl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/vfs.h> 
#include "configure.h"
#include "fstring.h"

#define CONF_PATH "/usr/local/http/conf.cfg"
#define CONF_SPLIT_STR "; "
#define MIN_PERC 1


void build_root_dir_len(struct conf *conf)
{
	int i;
	for (i=0; i<conf->nroot_dir; i++)
		conf->root_dir_len[i] = strlen(conf->root_dir[i]);
}

void subsp(char *ptrs[], int n, char *dst[][3])
{
	int i;
	for (i=0; i<n; i++)
		split(dst[i], ptrs[i], "| ");
}

void build_keylen(struct conf *conf)
{
	int i;
	for (i=0; i<conf->nkey; i++)
		conf->keylen[i] = strlen(conf->key[i]);
}

void link_hdr(struct conf *conf)
{
	int i;
	
	for (i=0; i < conf->nhdr; i++)
		conf->slist = curl_slist_append(conf->slist, conf->hdr[i]);
}

void build_map(struct conf *conf)
{
	int i;
	for (i=0; i<conf->nmap_str; i++)
	{
		conf->map[i] = (u_int)atoi(conf->map_str[i]);
		if (conf->map[i] >= conf->nroot_dir)
		{
			printf("map[%d] >= nroot_dir\n", conf->map[i]);
			_exit(0);
		}
	}
}

void build_ava_disk(struct conf *conf)
{
	int i, n = 0;
	u_int perc;
	u_int maxfree = 0;
	u_int who = 0;
	struct statfs st;
	for (i=0; i<conf->nroot_dir; i++)
	{
		if (statfs(conf->root_dir[i], &st) != 0)
			continue;
		perc = ((long long)st.f_bavail)*st.f_bsize/1024/1024/1024;
		if (perc == 0)
			continue;
		conf->ava_root_dir[n++] = i;
		if (st.f_bavail > maxfree)
		{
			maxfree = st.f_bavail;
			who = i;
		}
	}
	// how if n == 0 ? I just ignore it
	for (i=0; i<HIT_LEN; i++)
		conf->ava_root_dir[n++] = who;
	conf->nava = n;
	
}

void free_config(struct conf *conf)
{
	if (conf->slist)
	{
		curl_slist_free_all(conf->slist);
		conf->slist = 0;
	}
}
void read_config_file(struct conf *conf)
{
	memset(conf, 0, sizeof(struct conf));
	ReadConfig(CONF_PATH,"root_dir",conf->buff);
	conf->nroot_dir = split(conf->root_dir, conf->buff, CONF_SPLIT_STR);
	build_root_dir_len(conf);
	build_ava_disk(conf);

	ReadConfig(CONF_PATH, "map", conf->buff_map);
	conf->nmap_str = split(conf->map_str, conf->buff_map, " ");
	build_map(conf);

	ReadConfig(CONF_PATH, "use_map", conf->buff_use_map);
	conf->use_map = atoi(conf->buff_use_map);

	ReadConfig(CONF_PATH, "additional_hdr", conf->buff2);
	conf->nhdr = split(conf->hdr, conf->buff2, ";");
	link_hdr(conf);

	ReadConfig(CONF_PATH, "content_type", conf->buffcontyp);
	conf->ncontyp = split(conf->contyp, conf->buffcontyp, CONF_SPLIT_STR);
	subsp(conf->contyp, conf->ncontyp, conf->contyp_sp);

	ReadConfig(CONF_PATH, "omit_url_parameter", conf->buffkey);
	conf->nkey = split(conf->key, conf->buffkey, CONF_SPLIT_STR);
	build_keylen(conf);

	ReadConfig(CONF_PATH, "prefix", conf->buffprefix);
	conf->prefix_len = strlen(conf->buffprefix);

	ReadConfig(CONF_PATH, "disk_space", conf->buff_disk_space);
	conf->disk_space = atoi(conf->buff_disk_space);

	ReadConfig(CONF_PATH, "day", conf->buff_day);
	conf->day = atoi(conf->buff_day);
}

void read_config_file_simple(struct conf *conf)
{
	memset(conf, 0, sizeof(struct conf));
	ReadConfig(CONF_PATH,"root_dir",conf->buff);

	ReadConfig(CONF_PATH, "map", conf->buff_map);

	ReadConfig(CONF_PATH, "use_map", conf->buff_use_map);

	ReadConfig(CONF_PATH, "additional_hdr", conf->buff2);

	ReadConfig(CONF_PATH, "content_type", conf->buffcontyp);

	ReadConfig(CONF_PATH, "omit_url_parameter", conf->buffkey);

	ReadConfig(CONF_PATH, "prefix", conf->buffprefix);

	ReadConfig(CONF_PATH, "disk_space", conf->buff_disk_space);

	ReadConfig(CONF_PATH, "day", conf->buff_day);
}


